<?php


namespace app\admin\controller\auth;


use app\admin\controller\Base;
use app\common\libray\Tree;
use app\common\model\AuthGroup;
use app\common\model\AuthRule;
use think\facade\View;

class Group extends Base
{
    protected $groupList = [];
    protected $childrenGroupIds = [];
    protected $noNeedRight = ['ruleList'];

    public function initialize()
    {
        parent::initialize();
        $this->model = new AuthGroup();
        $this->childrenGroupIds = $this->auth->getChildrenGroupIds(true);
        $currentGroups = $this->auth->getGroups()->toArray();
        $currentGroupIds = array_column($currentGroups, 'group_id');
        //判断组别
        $groupList = $this->model->where(['status' => 1])->select($this->childrenGroupIds)->toArray();
        Tree::instance()->init($groupList);
        $result = [];
        if ($this->auth->isSuperAdmin()) {
            $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
        } else {
            $groups = $this->auth->getGroups();
            foreach ($groups as $m => $n) {
                $result = array_merge($result, Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['pid']), 'title'));
            }
        }
        $this->groupList = $result;
        View::assign('group_list', $this->groupList);
        assign_config('current_group_ids', $currentGroupIds);

    }

    public function index()
    {
        if ($this->request->isAjax()) {
            $total = count($this->groupList);
            $list = $this->groupList;
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return View::fetch();
    }

    public function ruleList()
    {
        $id = $this->request->param("id");
        $pid = $this->request->param("pid");

        $parentGroup = AuthGroup::find($pid);
        $currentGroup = null;
        if ($id) {
            $currentGroup = AuthGroup::find($id);
        }

        if (($pid || $parentGroup) && (!$id || $currentGroup)) {
            $id = $id ? $id : null;
            $ruleList = AuthRule::order('id asc')->select()->toArray();
            $parentRuleList = [];
            if (in_array('*', explode(',', $parentGroup->rules))) {
                $parentRuleList = $ruleList;
            } else {
                $parentRuleIds = explode(',', $parentGroup->rules);
                foreach ($ruleList as $k => $v) {
                    if (in_array($v['id'], $parentRuleIds)) {
                        $parentRuleList[] = $v;
                    }
                }
            }
            $ruleTree = new Tree();
            $groupTree = new Tree();
            //当前所有正常规则列表
            $ruleTree->init($parentRuleList);
            $authGroupList = AuthGroup::where(['status' => 1])->select($this->childrenGroupIds)->toArray();
            $groupTree->init($authGroupList);
            //读取当前角色下规则ID集合
            $adminRuleIds = $this->auth->getRuleIds();
            //是否是超级管理员
            $superadmin = $this->auth->isSuperAdmin();
            //当前拥有的规则ID集合
            $currentRuleIds = $id ? explode(',', $currentGroup->rules) : [];
            //组合数据
            $parentRuleList=[];
            if (!$id || !in_array($pid, $this->childrenGroupIds) || !in_array($pid, $groupTree->getChildrenIds($id, true))) {
                $parentRuleList = $ruleTree->getTreeList($ruleTree->getTreeArray(0));
                $parentRuleIds = array_column($parentRuleList, 'id');
                //这个是当前父节点所拥有的权限列表

                foreach ($parentRuleList as $k => &$v) {
                    if (!$superadmin && !in_array($v['id'], $adminRuleIds)) {
                        continue;
                    }
                    if ($v['pid'] && !in_array($v['pid'], $parentRuleIds)) {
                        continue;
                    }
                    $checked = in_array($v['id'], $currentRuleIds);
                    $open = in_array($v['pid'], $parentRuleIds);
                    $v['open']=$open;
                    $v['check']=$checked;
                }
                unset($v);
            }
            $this->success('权限重组成功', '', $parentRuleList);
        }

    }
}